<?php

class Client_Table_Projects extends Apk_Db_Table
{
	protected $_name      = 'projects';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }
    
    public function GetDataList($params=array())
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name)      		      		  
    		  ->where($this->_name.'.is_active = 1')
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0')
    		  ->order($this->_name.'.public_date desc');
    		  
    	// **  Params  **
    	if(count($params) > 0)	  
    	foreach ($params as $k => $v)
    	switch($k)
    	{
    		case 'cats':
    			$sel->where('id_cat in (?)', $v);
    			break;
    		default:	
    			$sel->where($this->_name.'.'.$k.' = ?', $v);
    	} 
   		  
    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }
    
    public function GetLast($limit = 3)
    {    	
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('is_active')
    				->where('is_removed = 0')
    				->where('is_ghost = 0')    				    				
    				->order('public_date desc')
    				->limit($limit);    	

    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    } 
    
    public function GetTop()
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->join('categories', 'categories.id = projects.id_cat', array('cat_name' => 'menu_name_ru'))
    				->where($this->_name.'.is_active')
    				->where($this->_name.'.is_removed = 0')
    				->where($this->_name.'.is_ghost = 0')
    				->where($this->_name.'.is_top = 1')    				
    				->order($this->_name.'.public_date desc');
    				//->limit(5);    	    	

    	return $this->setDecorator('topDecorator')->getList($sel)->toArray();
    } 
    
    public function GetPopular($params = array(), $fullperiod = false)
    {
    	$time_arr = getdate();
    	$public_date = mktime(0,0,0, $time_arr['mon']-2, $time_arr['mday'], $time_arr['year']);    	
    	
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('is_active')
    				->where('is_removed = 0')
    				->where('is_ghost = 0')
    				->where('is_top = 0')
    				->where('is_public = 1')
    				->where('type = 0')    				
    				->order(array('articles.rating desc', 'articles.rating_count desc', 'articles.public_date desc'))    				
    				->limit(5); 
    				
    	if(!$fullperiod) $sel->where('public_date > ?', $public_date);    				
    				
    	if(count($params) > 0)			
    	foreach($params as $k => $item)
    	switch ($k)    		
    	{
    		case 'cats': 
    			$sel->where('id_cat in (?)', $item);
    			break;
    		default:	
    			if($item > 0)
    				$sel->where($k.' = ?', $item);
    			break;	
    	}    		
    	
    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }    

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)    				
		    		->where($this->_name.'.is_active')		    		
		    		->where($this->_name.'.is_removed = 0')
		    		->where($this->_name.'.is_ghost = 0')
    				->where($this->_name.'.id = ?', $id);       	
    				
		try {
	    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
		}
		catch (Exception $e)
		{
			return array();
		}
		
    	$data['tags'] = $this->GetTags($id);
    	return $data;
    }    
       
    public function GetTags($id)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('tags_to_articles')   
    		  ->join('tags', 'tags.id = tags_to_articles.id_tag', array('id', 'name'))
    		  ->where('tags.is_active')
    		  ->where('tags_to_articles.id_articles = ?', $id);
    		  
    	return $this->getList($sel)->toArray();    		    	
    }
    
    public function GetComments($id)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles_comments')   
    		  ->join('accounts', 'accounts.id = articles_comments.id_acc', array('username', 'path', 'file_name'))
    		  ->where('articles_comments.id_article = ?', $id);    		  
    		  
    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();    		    	
    }    
    
    public function AddHit($id)
    {
    	$data = $this->GetDataRow($id);
    	
    	$d['hit_count'] = $data['hit_count'] + 1;
    	$this->update($d, 'id = '.$id);
    		  
    	return true;    		    	
    } 
    
    public function is_rated($id, $ip_addr)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles_rating')
    		  ->where('ip_addr = ?', $ip_addr)
    		  ->where('id_article = ?', $id);
    		  
    	return $this->getList($sel)->count();
    }
    
    public function rating($data)
    {
    	if($this->is_rated($data['id_article'], $data['ip_addr'])) return false;
    	
    	$data['date'] = time();
    	
    	$this->getAdapter()->insert('articles_rating', $data);    	
    	$this->calc_rate($data['id_article']);
    	return true;
    }   
    
    public function calc_rate($id)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles_rating', array('count' => 'count(id)', 'sum' => 'sum(value)'))
    		  ->where('id_article = ?', $id);
    		  
    	$data = $this->getList($sel)->getRow(0);
    	
    	$d['rating_count'] = $data->count;
    	if($data->count > 0) $d['rating'] = $data->sum / $data->count;
    	else $d['rating'] = 0;
    	    	
    	parent::update($d, 'id = '.$id);   	
    }
    
    public function comment($data)
    {    		  
    	$data['add_date'] = time();    	
    	$this->getAdapter()->insert('articles_comments', $data);   	
    	return $this->getAdapter()->lastInsertId('articles_comments');
    }     
    
    public function GetAccessList($a = null, $lang = 'ru')
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					->from('access_level', array('id', 'title' => 'title_'.$lang))
					->order('title_ru');		
					
		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    } 
      
    

    
    //**********************************************
    // insert/update/delete    
    
	public function insert(array $data, $user_id)
    {
    	$data['public_date'] = time();
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;
    	$data['is_active'] = 1;
    	
        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id = 0)
    {
    	$data['modify_date'] = time();    	
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

    	//if(!isset($data['is_active'])) $data['is_active'] = 0;  	
    	//if(!isset($data['is_commented'])) $data['is_commented'] = 0;  	
    	//if(!isset($data['is_rated'])) $data['is_rated'] = 0;  	
    	
		return parent::update($data, 'id = '.$id);
    }

    public function remove($items, $user_id)
    {    	
    	foreach($items as $k => $id)
    	{
	    	$data['modify_user_id'] = $user_id; 
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;
	    	
	    	parent::update($data, 'id = '.$id);    		
    	}
    	
    	return true;    	
	}
	
    public function remove_commetns($items, $aid)
    {    	
    	foreach($items as $k => $id)
    	{	    	
	    	$this->getAdapter()->delete('articles_comments', 'id = '.$id);    		
    	}
    	
    	$this->commetns_count($aid);
    	
    	return true;    	
	}	
	
    public function commetns_count($id)   
    {
    	$sel = $this->getAdapter()->select()
    					->from('articles_comments', array('count' => 'count(id)'))
    					->where('id_article = ?', $id);
    	$count = $this->getList($sel)->getRow(0)->toArray();  
    	
    	$d['comments_count'] = $count['count'];
    	parent::update($d, 'id = '.$id);     	
    }    


    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }
    
    public function topDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;            	

            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }    

}
?>